qui(
    function (require, exports, module) {

        Ticket.Grid = function (options) {
            this.dao = Ticket.Dao;
            this.config = Ticket.Config;
            this.util = Ticket.Util;
            this.task = Ticket.Task;
            this.options = options || {};
        };

        Com.inherits(Ticket.Grid, Ancestor.Grid).extend({
            doInit: function () {
                var self = this;
                self.$grid = self.options.$grid;
                self.$logGrid = self.options.$logGrid;
                var taskArr = [];
                taskArr.push(self.task.getConfigType(self.dao.cfgName.logType));
                taskArr.push(self.task.getConfigType(self.dao.cfgName.workType));
                $.each(taskArr, function (index, element) {
                    EpgWeb.ajaxQueue.push(element.regName, element.options);
                })
                EpgWeb.ajaxQueue.afterLoadComplete(function () {
                    self.startGrid();
                    var test = Com.url.getParameter("test");
                    if (test == "1") {
                        Ticket.Util.transToDevMode();
                    }
                })
                EpgWeb.ajaxQueue.startLoad();
            },

            startGrid: function () {
                var self = this;
                self.initTicketDataGrid();
                self.initHistoryGrid();
                self.initLogGrid();
                self.initDlg();
                self.bindEvent();
                self.initSuggest();
                $("button[name=queryBtn]").click();
            },
            bindEvent: function () {
                var self = this;
                $("button[query-sql]").bind("click", function (event) {
                    var $target = $(event.currentTarget);
                    $("button[type=reset]").click();
                    var params = Ticket.Util.buildTicketDataParams($target.attr("query-sql").split(","), $target.attr("query-value").split(","), $target.attr("quick"))
                    if ($target.attr("quick") == "true") {
                        Ticket.Util.fillBasicForm(params);
                        $("button[name=queryBtn]").click();
                    } else {
                        Ticket.Util.getTicketDataList(params);
                    }
                });
                $("button[name=queryBtn]").bind("click", function (event) {
                    var params = self.getParams();
                    Ticket.Util.getTicketDataList(params);
                })

            },
            getParams: function () {
                var self = this;
                var params = {};
                var $elements = $.makeArray($("form[name=basicFrm]").prop('elements'));
                $.each($elements, function () {
                    if ($(this).val() == "") {
                        return true;
                    }
                    if (this.name == "deadline") {
                        self.decodeDeadline(params, $(this));
                    } else {
                        params[this.name] = $(this).val();
                    }
                });
                if ("EQS_reporter" in  params) {
                    params["EQS_reporter"] = $("input[name=EQS_reporter]").suggest("getValue");
                }
                if ("EQS_executor" in  params) {
                    params["EQS_executor"] = $("input[name=EQS_executor]").suggest("getValue");
                }
                return params;
            },
            decodeDeadline: function (params, jElement) {
                if (jElement.attr("decodemode") == "function") {
                    var selectValue = jElement.val();
                    var paramsArr =  Ticket.Util.decodeDeadline(selectValue);
                    $.each(paramsArr,function(i,obj){
                        params[obj.key] = obj.result;
                    })
                }
            },
            initTicketDataGrid: function () {
                var self = this;
                var gridConfig = self.getGridConfig(self.config.TICKET_DATA_LIST, self.config.TABLE_COLMODEL_CONFIG);
                self.$grid.jqGrid(gridConfig);
            },
            initHistoryGrid: function () {
            },
            initLogGrid: function () {
                var self = this;
                var gridConfig = self.getGridConfig(self.config.TABLE_LOG, self.config.TABLE_COLMODEL_CONFIG);
                self.$logGrid.jqGrid(gridConfig);
            },
            initDlg: function () {
                var self = this;
                // self.initCreateTicketDlg();
                self.initHistoryDlg();
                self.initWorkerDlg();
                self.initDlgCheckList();
                $(".ui-dialog-titlebar-close").html('<span class="ui-button-icon-primary ui-icon ui-icon-closethick"></span><span class="ui-button-text">close</span>');
            },
            initDlgCheckList: function () {
                $("#dlg_checkList").dialog({
                    title: "直播检查列表",
                    autoOpen: false,
                    width: 500,
                    modal: true,
                    dialogClass: 'alert',
                    resizable: false,
                    open: function () {
                        $("#form_checkList")[0].reset();
                        $("#form_checkList").find("a.qui-tag-remove").click();
                        $("#form_checkList").find("input[name=project_workers]").suggest("setValue", "");
                    }
                });
            },
            initCreateTicketDlg: function () {
                $("#createDiv").dialog({
                    title: "直播工单创建",
                    autoOpen: false,
                    width: 500,
                    modal: true,
                    dialogClass: 'alert',
                    resizable: false,
                    open: function () {
                        $("input[name=programQipuId]").removeAttr("readonly");
                        $("input[name=programQipuId]").val("");
                        Ticket.Model.clearModelView();
                    }
                });
            },
            initHistoryDlg: function () {
                $("#ticketLogDiv").dialog({
                    title: "历史记录",
                    autoOpen: false,
                    width: 900,
                    height: 600,
                    modal: true,
                    dialogClass: 'alert'
                });
            },
            initWorkerDlg: function () {
                $("#workerDiv").dialog({
                    title: "分配任务",
                    autoOpen: false,
                    width: 480,
                    modal: true,
                    dialogClass: 'alert'
                });
                var self = this;
                var workerData = self.dao.getWorkers();
                var selectJDom = $("select[name=assign_worker]");
                $("<option value=''>请选择</option>").appendTo(selectJDom);
                if (workerData.data != null) {
                    $.each(workerData.data, function (index, obj) {
                        $("<option>", {
                            value: obj.val,
                            html: obj.desc
                        }).appendTo(selectJDom);
                    });
                }
                $("button[name=worker-dlg-cancel]").bind("click", function (event) {
                    $("#workerDiv").dialog("close");
                })
                $("button[name=worker-dlg-ok]").bind("click", function (event) {
                    var selectWorker = $("select[name=assign_worker]").val();
                    if (selectWorker.length == 0) {
                        return;
                    }
                    var ticketId = parseInt($("input[name=assign_ticket_id]").val());
                    Ticket.Util.assignTask(ticketId, selectWorker);
                    $("#workerDiv").dialog("close");
                })
            },
            initSuggest: function () {
                var emailSuggest = {
                    options: {
                        source: Ticket.urlConfig.redirectUrl,
                        method: "post",
                        multiple: false,
                        getParam: function (term) {
                            var requestData = {url: Ticket.Constant.thirdUrl_query_user, type: "get", params: {email: term}, needCookie: true};
                            return {"request": JSON.stringify(requestData), timeSign: new Date().getTime()}
                        },
                        process: function (json) {
                            var ret = [],
                                self = this,
                                options = self.options,
                                itemData = null;
                            var data = null;
                            if (json == null) {
                                return [];
                            }
                            if (json.code === "A00000") {
                                data = json.data == null ? null : json.data;
                            }
                            if (!data) {
                                return [];
                            }
                            $.map(data, function (item) {
                                itemData = {};
                                itemData[options.valueKey] = item["email"];
                                itemData[options.labelKey] = item["userName"];
                                itemData["email"] = item["email"];
                                itemData["uid"] = item["uid"];
                                ret.push(itemData);
                            });
                            return ret;
                        },
                        template: function (item) {
                            return item.label + "(" + item.id + ")";
                        }
                    }
                }
                $("input[name=EQS_reporter]").suggest(emailSuggest.options);
                $("input[name=EQS_executor]").suggest(emailSuggest.options);

            },
            bindEventDebug: function () {
                $("button[name=create_ticket_data]").bind("click", function (event) {
                    $("#createDiv").dialog("open");
                })
                $("input[name=programQipuId]").focusout(function (event) {
                    var queryWord = $("input[name=programQipuId]").val();
                    if (queryWord.length > 2 && queryWord.endsWith("23")) {
                        if (Ticket.Model.underUsing()) {
                            return;
                        }
                        var sendData = { qipuId: queryWord, deleteStatus: 0, liveType: 2, fromPath: "library", pageNo: 1, pageSize: 1}
                        Ticket.Model.clearModelView();
                        Ticket.Dao.sendAjax("/api/program/list", "post", sendData, function (rdata) {
                            if (rdata.code === "A00000" && rdata.data.result.length > 0) {
                                $("input[name=programQipuId]").attr("readonly", true);
                                Ticket.Model.fillModelHtml(rdata.data.result[0]);
                            } else {
                                EpgWeb.Plugin.showTips({type: "error", content: rdata.msg == null ? "请求失败" : rdata.msg  });
                            }
                        }, "/program/list");
                    }
                })

            }
        });

    }
);